package org.sean.framework.parse.impl;

import org.sean.framework.logging.Logger;
import org.sean.framework.parse.InputStreamParser;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

/**
 * Date: 2016/11/21 20:29
 *
 * @author Sean.xie
 */
public class ByteArrayInputParser implements InputStreamParser<byte[]> {

    private final Logger logger = Logger.newInstance(ByteArrayInputParser.class);

    @Override
    public byte[] parser(InputStream inputStream) {
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
            int len = -1;
            byte[] bytes = new byte[1024 * 8];
            while ((len = inputStream.read(bytes)) != -1) {
                bos.write(bytes, 0, len);
            }
            return bos.toByteArray();
        } catch (Exception e) {
            logger.printStackTrace(e);
            return new byte[0];
        }
    }
}